package com.zwf.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zwf.constant.MessageConstant;
import com.zwf.context.BaseContext;
import com.zwf.dto.CategoryDTO;
import com.zwf.dto.CategoryPageQueryDTO;
import com.zwf.entity.Category;
import com.zwf.entity.Employee;
import com.zwf.exception.DeletionNotAllowedException;
import com.zwf.mapper.CategoryMapper;
import com.zwf.mapper.DishMapper;
import com.zwf.mapper.SetmealMapper;
import com.zwf.result.PageResult;
import com.zwf.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author zwf
 * @date 2024/1/30 19:01
 * @PackageName:com.zwf.service.impl
 * @ClassName:CategoryServiceImpl
 * @Description: TODO
 * @Version: 1.0
 */

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分页分类查询
     * @param categoryPageQueryDTO
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category> page=categoryMapper.pageQuery(categoryPageQueryDTO);
        long total=page.getTotal();
        List<Category> result=page.getResult();
        PageResult pageResult=new PageResult(total,result);
        return pageResult;
    }

    /**
     * 启用禁用分类
     * @param status
     * @param id
     */
    @Override
    public void enAndDisCategory(Integer status, Long id) {
        Category category=Category.builder()
                        .id(id)
                                .status(status)
                                        .build();
        categoryMapper.update(category);
    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void add(CategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        //新增菜品状态设为禁用，防止直接显示到用户端
        category.setStatus(0);
//        category.setCreateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.add(category);
    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    public void edit(CategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
//        category.setUpdateUser(BaseContext.getCurrentId());
//        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void delete(Long id) {
        //判断当前分类是否关联了菜品
        if (dishMapper.count(id)>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //判断当前分类是否关联了套餐
        if (setmealMapper.count(id)>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.delete(id);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> queryByType(Integer type) {
        List<Category> list=categoryMapper.queryByType(type);
        return list;
    }
}
